<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">特材供应商编码</span>
                <el-input v-model="tableParam.code" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">供应商名称</span>
                <el-input v-model="tableParam.supplier" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <el-button icon="el-icon-search" size="medium" class="bottom_all but1" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" @click="init()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="addFromDate('添加')">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='RIYONG'))!==-1" size="small" class="bottom_three" @click="Update(1)">
                <i class="icon iconfont iconicon_button_startuse" style="font-size: 12px;"></i>
                启用
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='JINYONG'))!==-1" size="small" class="bottom_four" @click="Update(0)">
                <i class="icon iconfont iconicon_button_disable1" style="font-size: 12px;"></i>
                禁用
              </el-button>
            </div>
          </div>
          <div v-loading="tableLoading" class="card_info">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection"></el-table-column>
              <template v-for="(item, index2) in table">
                <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed=" (item.label =='操作'||item.label =='二维码')?'right':false" show-overflow-tooltip>
                  <template slot-scope="scope">
                    <span v-if="scope.row.enable == 0 && item.label == '状态'" style="color:#969799;">禁用</span>
                    <span v-if="scope.row.enable == 1 && item.label == '状态'" style="color:#00A754;">启用</span>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="updateFromDate(scope.row, '修改')">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                    <span v-if="item.label !== '操作' && item.label !== '状态'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
          <div class="card_bottom">
            <div class="card_bottom_left">
              <div class="top_page_totle">总共{{ total }}条</div>
              <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
            </div>
            <div>
              <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="580px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" style="padding: 20px;" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="companyId">
            <span slot="label" style="color:  #FF0000 ;">所属单位</span>
            <el-select ref="companyId" v-model="fromDate.companyId" placeholder="请选择" @change="$nextTick(()=>{fromDate.company = $refs.companyId.selectedLabel})">
              <el-option v-for="item in companyIdList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="supplier">
            <span slot="label" style="color:  #FF0000 ;">供应商名称</span>
            <el-input v-model="fromDate.supplier" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="supplierTypeId">
            <span slot="label" style="color:  #FF0000 ;">供应商类型</span>
            <el-select ref="supplierTypeId" v-model="fromDate.supplierTypeId" placeholder="请选择" @change="$nextTick(()=>{fromDate.supplierType = $refs.supplierTypeId.selectedLabel})">
              <el-option v-for="item in typelist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="linkman">
            <span slot="label" style="color:  #FF0000 ;">联系人</span>
            <el-input v-model="fromDate.linkman" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <span slot="label" style="color:  #FF0000 ;">联系电话</span>
            <el-input v-model="fromDate.phone" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item>
            <span slot="label">备注</span>
            <el-input v-model="fromDate.remark" :rows="3" resize="none" style="width: 280px;" type="textarea" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="enable">
            <el-radio-group v-model="fromDate.enable">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  batchUpdateOrDelete,
  saveOrUpdate,
} from "@/api/smart/specialmaterial/supplier.js";
import { permissionsCompanyList, getDicType } from "@/api/common.js";
export default {
  mixins: [mixin],
  data() {
    var phone0 = (rule, value, callback) => {
      let reg = new RegExp(/^[1-9]\d{10}$/)
      console.log(value, reg.test(value));
      if (reg.test(value)) {
        callback();
      } else {
        callback(new Error("请输正确的手机号!"));
      }
    };
    return {
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      batchUpdateOrDelete: batchUpdateOrDelete,
      saveOrUpdate: saveOrUpdate,
      /*表格数据*/
      table: [
        {
          label: "特材供应商编码",
          width: "120",
          prop: "code",
          status: true,
        },
        {
          label: "供应商名称",
          width: "auto",
          prop: "supplier",
          status: true,
        },
        {
          label: "供应商类型",
          prop: "supplierType",
          status: true,
        },
        {
          label: "联系人",
          width: "auto",
          prop: "linkman",
          status: true,
        },
        {
          label: "联系电话",
          width: "100",
          prop: "phone",
          status: true,
        },
        {
          label: "备注",
          width: "auto",
          prop: "remark",
          status: true,
        },
        {
          label: "状态",
          width: "100",
          prop: "",
          status: true,
        },
        {
          label: "操作",
          width: "140",
          prop: "",
          status: true,
        },
      ],
      /*添加表单数据*/
      rules: {
        supplier: [
          { required: true, message: "请输入供应商名称", trigger: "change" },
        ],
        supplierTypeId: [
          { required: true, message: "请选择供应商类型", trigger: "change" },
        ],
        companyId: [
          { required: true, message: "请选择所属单位", trigger: "change" },
        ],
        linkman: [
          { required: true, message: "请输入联系人", trigger: "change" },
        ],
        phone: [
          { required: true, validator: phone0, trigger: "change" },
        ],
      },
      fromDate: {
        linkman: "",
        phone: "",
        supplier: "",
        supplierType: "",
        carTypeName: "",
        company: "",
        companyId: "",
        supplierTypeId: "",
        enable: 1,
        remark: ''
      },
      companyIdList: [],
      typelist: [],
    };
  },
  async created() {
    this.companyIdList = await permissionsCompanyList()
    this.tableParam = { ...this.tableParam, code: "", supplierType: "" };
    this.gettableData();
    getDicType({ code: "supplierType" }).then((res) => {
      this.typelist = res.data.items;
    });
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  methods: {
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          _this.submitFromDate();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    init() {
      /*重置数据*/
      this.tableParam.code = "";
      this.tableParam.supplier = "";
      this.gettableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.dialoginfo {
  .el-input,
  .el-select {
    width: 280px;
  }
}
</style>
